package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.ColorOrSizeModel;
import com.tiantianyuegou.model.SpecListModel;
import com.tiantianyuegou.view.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ivany on 2016/1/22.
 */
public class SpecListAdapter extends BaseAdapter {

    private Context mContext;
    private List<SpecListModel> mData;

    public interface IChangeSpecListener {
        void changeSpec(int postition, ColorOrSizeModel model);
    }

    private IChangeSpecListener mListener;

    public void setChangeSpecListener(IChangeSpecListener listener) {
        mListener = listener;
    }

    public SpecListAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setData(List<SpecListModel> data) {
        mData.clear();
        mData.addAll(data);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHoldr holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_product_spec, parent, false);
            holder = new ViewHoldr();
            holder.mTxt_title = (TextView) convertView.findViewById(R.id.item_product_spec_title);
            holder.mGridView = (NoScrollGridView) convertView.findViewById(R.id.item_product_spec_select);
            convertView.setTag(holder);
        } else {
            holder = (ViewHoldr) convertView.getTag();
        }
        SpecListModel model = mData.get(position);
        holder.mTxt_title.setText(model.getTitle());
        final List<ColorOrSizeModel> data = model.getValues();
        final ColorOrSizeGridAdapter mAdapter = new ColorOrSizeGridAdapter(mContext, data);
        holder.mGridView.setAdapter( mAdapter);
        holder.mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int gridPosition, long id) {
                for (int i = 0; i < data.size(); i++) {
                    data.get(i).setIsSelect(false);
                }
                data.get(gridPosition).setIsSelect(true);
                mAdapter.notifyDataSetChanged();
                mListener.changeSpec(position,data.get(gridPosition));
            }
        });
        return convertView;
    }

    class ViewHoldr {
        TextView mTxt_title;
        NoScrollGridView mGridView;
    }
}
